package com.dfd.store.web.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dfd.store.domain.Admin;
import com.dfd.store.service.AdminService;
import com.dfd.store.service.impl.AdminServiceImpl;
import com.dfd.store.utils.MyBeanUtils;
import com.dfd.store.web.base.BaseServlet;

public class AdminLoginServlet extends BaseServlet {
    // 管理员的登录方法
    public String adminlogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Admin admin = new Admin();
        MyBeanUtils.populate(admin, request.getParameterMap());
        AdminService adminService = new AdminServiceImpl();
        Admin admin1 = null;
        try {
            // 登陆成功
            admin1 = adminService.login(admin);
            request.getSession().setAttribute("existadmin", admin1);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            System.out.println(msg);
            // 向request放入失败的信息
            request.getSession().setAttribute("msg", msg);
            return "/admin/index.jsp";
        }
        return "/admin/home.jsp";
    }
}
